package pri.jz.keeper.ui.base

import android.graphics.Color
import android.os.*
import android.util.Log
import android.view.KeyEvent
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.core.view.WindowCompat
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import com.google.accompanist.insets.ProvideWindowInsets
import kotlinx.coroutines.launch
import pri.jz.keeper.eventbus.EventBusUtil
import pri.jz.keeper.jui.jdialog.JLoadingDialog
import pri.jz.keeper.jui.jdialog.JTipDialog
import pri.jz.keeper.ui.theme.KeeperTheme
import pri.jz.keeper.util.ActivityUtil

/**
 *Author by: JZ
 *Email: 2451756617@qq.com
 *PS:Activity的父类
 */
abstract class BaseActivity : ComponentActivity(){
    protected lateinit var TAG: String

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initData()
        Log.d(TAG, "onCreate:$TAG ")
        setContent {
            KeeperTheme {
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colors.background
                ){
                    ProvideWindowInsets {
                        JTipDialog(viewModel = getViewModel())
                        JLoadingDialog(viewModel = getViewModel())
                        OnCreateView()
                    }
                }
            }
        }
    }

    /**
     * 在子类中界面不再写在Create中  请在此方法中重写
     * 尽量不要在此方法中写入过多于UI无关的编码，因为此方法会频繁重组
     * 想要初始化Activity请写在子类的initView方法中
     */
    @Composable
    protected abstract fun OnCreateView()
    //实现状态观察
    protected abstract suspend fun onObserver()
    //在子类中重写此方法可以来做到共享Viewmodel 如果不重写则无法调用加载框
    protected open fun getViewModel(): BaseViewModel = ViewModelProvider(this)[BaseViewModel::class.java]

    //初始化界面属性
    protected open fun initData() {
        TAG = javaClass.simpleName
        if (resigterEventBus()) EventBusUtil.register(this)
        ActivityUtil.addActivity(this)
        lifecycleScope.launchWhenStarted {
            onObserver()
        }
    }

    /**
     * 实现沉浸式状态栏
     * @param isAppearanceLightStatusBars 状态栏字体颜色是否为浅色
     */
    protected open fun fullScreen(
        isLightStatusBarsColor: Boolean = true
    ) {
        window.apply {
            statusBarColor = Color.TRANSPARENT
        }
        WindowCompat.getInsetsController(window,window.decorView)
            .isAppearanceLightStatusBars = !isLightStatusBarsColor
        WindowCompat.setDecorFitsSystemWindows(window,false)
    }

    //是否注册EventBus
    protected open fun resigterEventBus(): Boolean = false

    /**
     * 监听滑动两次退出
     * @param keyCode
     * @param event
     * @return
     */
    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        return if (keyCode == KeyEvent.KEYCODE_BACK && ActivityUtil.getActivitySize() == 1) {
            ActivityUtil.exitApp(this)
            true
        } else super.onKeyDown(keyCode, event)
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBusUtil.unRegister(this)
        ActivityUtil.removeActivity(this)
    }


}